package com.sxx.beautifulcity.adapter;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.LineData;
import com.sxx.beautifulcity.entity.greendao.LineDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnLineItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class LineAdapter extends RecyclerView.Adapter<LineAdapter.LineAdapterViewHolder>{

    private List<LineDaoEntity> datas;
    private OnLineItemClickListener listener;

    public void setOnLineItemClick(OnLineItemClickListener l){
        listener = l;
    }


    public void addData(List<LineDaoEntity> list){
        datas = list;
        notifyDataSetChanged();
    }


    class LineAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView name_lineTv;
        private SimpleDraweeView draweeView_line;
        private LinearLayout linear;
        private int position;

        public LineAdapterViewHolder(View itemView) {
            super(itemView);
            draweeView_line = (SimpleDraweeView) itemView.findViewById(R.id.draweeView_line);
            name_lineTv = (TextView) itemView.findViewById(R.id.name_lineTv);
            linear = (LinearLayout) itemView.findViewById(R.id.linear);
            linear.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            if (datas != null && datas.size() > 0) {
                LineDaoEntity data = datas.get(position);
                if (listener != null) {
                    listener.OnLineItemClick(data);
                }
            }
        }
    }

    /***
     * 加载行布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public LineAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_line_item, null);
        return new LineAdapterViewHolder(view);
    }

    /***
     * 绑定数据
     * @param holder
     * @param position
     */

    @Override
    public void onBindViewHolder(LineAdapterViewHolder holder, int position) {
        if (datas !=null && datas.size() > 0) {
            LineDaoEntity data = datas.get(position);
            holder.name_lineTv.setText(data.getName());
            String path = data.getImage();
            frescoImage(path , holder.draweeView_line);
        }
        holder.position = position;
    }

    public void frescoImage(String path , SimpleDraweeView draweeView){
        Uri uri = Uri.parse(path);
        draweeView.setImageURI(uri);
    }

    /***
     * 返回行布局行数
     * @return
     */
    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }
}
